/****************************************************************************
**
** This file is part of qPhotoTag, a photo keywording application
**
** Copyright (C) 2009 Marcell Lengyel <miketkf@gmail.com>
**
** GNU General Public License
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**
****************************************************************************/

/*! \file taglist.cpp
    \brief This file contains the implementation of the TagList class

    The TagList class is a QListView subclass that is used by the
    qPhotoTag application to display a list of IPTC keywords.
 */
#include <QtGui>
#include "taglist.h"

TagList::TagList(QWidget *parent)
        : QTreeView(parent)
{
    this->setHeaderHidden(true);
    tag_list_model = new QStandardItemModel(this);
    this->setModel(tag_list_model);
    this->setSelectionMode(QAbstractItemView::ExtendedSelection);
    this->setEditTriggers(QAbstractItemView::NoEditTriggers);
    this->setUniformRowHeights(true);
    this->setExpandsOnDoubleClick(false);
#ifdef Q_WS_MAC
    this->setFrameStyle(QFrame::NoFrame);
    this->setAlternatingRowColors(true);
#endif
}

void TagList::focusInEvent(QFocusEvent *e)
{
    emit tagListFocusIn();
    QTreeView::focusInEvent(e);
}

QStringList TagList::get_child_tags(QModelIndex &parent)
{
    QStringList childTags;
    if (! tag_list_model->hasChildren(parent))
        return childTags;

    int numRows = tag_list_model->rowCount(parent);
    for (int row = 0; row < numRows; ++row) {
        QModelIndex index = tag_list_model->index(row, 0, parent);
        QString tag = tag_list_model->data(index, Qt::DisplayRole).toString();
        childTags.append(tag);
        if (tag_list_model->hasChildren(index)) {
            childTags.append(get_child_tags(index));
        }
    }
    return childTags;
}

QStringList TagList::get_tags()
{
    QStringList allTags;
    int numRows = tag_list_model->rowCount();
    for (int row = 0; row < numRows; ++row) {
        QModelIndex index = tag_list_model->index(row, 0);
        QString tag = tag_list_model->data(index, Qt::DisplayRole).toString();
        allTags.append(tag);
        if (tag_list_model->hasChildren(index)) {
            // get the tags for all child tags
            allTags.append(get_child_tags(index));
        }
    }
    return allTags;
}

void TagList::set_tags(const QStringList &newTags)
{
    this->clear();
    if (! newTags.isEmpty()) {
        QStandardItem *rootItem = tag_list_model->invisibleRootItem();
        for (QStringList::const_iterator it = newTags.constBegin(); it != newTags.constEnd(); ++it ) {
            QStandardItem *item = new QStandardItem(*it);
            rootItem->appendRow(item);
        }
    }
}

void TagList::update_hierarchy(const QStringList &hierarchicalTagList) {
    if (! hierarchicalTagList.isEmpty()) {
        for (QStringList::const_iterator it = hierarchicalTagList.constBegin(); it != hierarchicalTagList.constEnd(); ++it ) {
            // Split the hiearchical tags from Country|City|Area style
            QStringList hierarchicalTags = QString(*it).split("|",QString::SkipEmptyParts);
            for (int i = 1; i < hierarchicalTags.size(); ++i) {
                QList<QStandardItem *> parentList = tag_list_model->findItems(hierarchicalTags.at(i-1),Qt::MatchRecursive);
                QList<QStandardItem *> childList = tag_list_model->findItems(hierarchicalTags.at(i),Qt::MatchRecursive);
                // parent and child is already in the tag list
                // and parent does not yet own child
                if (! parentList.isEmpty() && ! childList.isEmpty() &&
                    (parentList.first()->index() != tag_list_model->parent(childList.first()->index()))) {
                    int childRow = childList.first()->row();
                    QStandardItem *child = tag_list_model->takeItem(childRow);
                    tag_list_model->removeRow(childRow);
                    parentList.first()->appendRow(child);
                    this->setExpanded(parentList.first()->index(), true);
                }
            }
        }
    }
}

void TagList::add_tag(const QString &newTag)
{
	if (! newTag.isEmpty()) {
            QStandardItem *rootItem = tag_list_model->invisibleRootItem();
            if (tag_list_model->findItems(newTag, Qt::MatchRecursive).isEmpty()) {
                    QStandardItem *item = new QStandardItem(newTag);
                    rootItem->appendRow(item);
                }
	}
}

void TagList::clear()
{
    tag_list_model->clear();
}

bool TagList::is_selection()
{
	bool retval = false;
	if (! this->selectionModel()->selectedIndexes().empty())
		retval = true;
	return retval;
}

QStringList TagList::get_selected_tags()
{
	QStringList selected_tags;
	if (is_selection())
	{
		QModelIndex index;
		foreach(index, this->selectionModel()->selectedIndexes())
		{
                        selected_tags.append(tag_list_model->data(index,Qt::DisplayRole).toString());
                        // Add all parent tags to the selected list as well
                        // TODO
		}
	}
	return selected_tags;
}

void TagList::remove_selected_rows()
{
	
}
